//
//  AppDelegate.swift
//  iOSSinaWeb
//
//  Created by yws on 16/5/9.
//  Copyright © 2016年 yws. All rights reserved.
//

import UIKit

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        
    
        setupAppearance()
       
        window = UIWindow(frame:  UIScreen.mainScreen().bounds)
     
        //添加根控制器
//        window?.rootViewController = tabBarVC
        
        //测试控制器
        window?.rootViewController = defaultRootViewControler()
        
        
        
        //设置为主窗口
        window?.makeKeyAndVisible()
        
        

        return true
    }
    
    func defaultRootViewControler() -> UIViewController {
        if !AYUserAccountViewModel.sharedInstance.isUserLogin {
            return AYMainViewController()
        }
        
        if isNewVersion() {
            return AYNewFeatureViewController()
        } else {
            return AYWelcomeViewController()
        }
        
    }
    
    
    
    ///选择根控制器
    func switchRootViewController(controller: UIViewController) {
        
        window?.rootViewController = controller
    }
    
    ///判断当前版本是不是新版
    private func isNewVersion() -> Bool {
        
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        
        let sandboxVersionKey = "sandboxVersionKey"
        
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandboxVersionKey)
        
        let isNewVersion = currentVersion != sandboxVersion
        
        if isNewVersion {
            NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: sandboxVersionKey)
            
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        return isNewVersion
    }
    
    
    
    

    //设置Bar控制器的颜色
    private func setupAppearance() {
        //这方法返回的是自己(NavigationBar)
        let bar = UINavigationBar.appearance()
        bar.tintColor = UIColor.orangeColor()
//
    }
    
  
}

